//
//  ScreeningViewController.m
//  CheekiOS
//
//  Created by mac on 2017/5/17.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ScreeningViewController.h"
#import "MARKRangeSlider.h"
@interface ScreeningViewController ()

@property (nonatomic, strong) NSMutableArray *btnsArr;
@property (nonatomic, strong) UIButton  *sexBtn;
@property (nonatomic, strong) UILabel *userTextLab;
@property (nonatomic, strong) MARKRangeSlider *sliderAge;

@property (nonatomic, strong) UILabel *ageLabel;
@property (nonatomic, strong) UISwitch *SwitchBtn;

@end

@implementation ScreeningViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ColorI(0xf4f3f2);
    
    _priorityStr = @"1";
    _sexStr = @"2";
    
    _btnsArr = [NSMutableArray new];
    

    

    CGFloat buttonY ;
    CGFloat titleY;
    
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
        NSLog(@"ipad");
        buttonY = 16;
        titleY = 0;
    }else{
        buttonY = 26;
        titleY = 20;
        NSLog(@"iphone or ipod");
    }
    
    
    
    UIView *barView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 64)];
    barView.backgroundColor = Colorbackground;
   //返回
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(15, buttonY, 34*SCREEN_WIDTH_RATIO47, 34*SCREEN_WIDTH_RATIO47)];
    [button setTitle:@"取消" forState:UIControlStateNormal];
    button.titleLabel.font = FontP(14);
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [barView addSubview:button];
    [self.view addSubview:barView];
    //筛选
    UILabel *titlelab = [[UILabel alloc] initWithFrame:CGRectMake(KScreenWidth/4, titleY, KScreenWidth/2, 35*SCREEN_WIDTH_RATIO47)];
    titlelab.text = @"筛选";
    titlelab.textColor = [UIColor blackColor];
    titlelab.font = FontP(18);
    titlelab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titlelab];
    //确定
    UIButton *determineBtn = [[UIButton alloc]initWithFrame:CGRectMake(barView.right-49, buttonY, 34*SCREEN_WIDTH_RATIO47, 34*SCREEN_WIDTH_RATIO47)];
    [determineBtn setTitle:@"确定" forState:UIControlStateNormal];
    determineBtn.titleLabel.font = FontP(14);
    [determineBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [determineBtn addTarget:self action:@selector(determineBtn:) forControlEvents:UIControlEventTouchUpInside];
    [barView addSubview:determineBtn];
    
    //想看
    _userTextLab = [[UILabel alloc] initWithFrame:CGRectMake(15, barView.bottom, 100*SCREEN_WIDTH_RATIO47, 30*SCREEN_WIDTH_RATIO47)];
    _userTextLab.text = @"想看到的用户";
    _userTextLab.font = FontP(12);
    _userTextLab.textColor = [UIColor blackColor];
    [self.view addSubview:_userTextLab];
    
    //按钮
    NSArray * titles = @[@"全部",@"男",@"女"];
    CGFloat btnW = KScreenWidth/titles.count;
    CGFloat btnH = 40*SCREEN_WIDTH_RATIO47;
    for (int i = 0; i < titles.count; i++) {
        CGFloat btnX = i*btnW;
        _sexBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sexBtn setTitle:titles[i] forState:UIControlStateNormal];
        [_sexBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_sexBtn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        _sexBtn.frame = CGRectMake(btnX,  _userTextLab.bottom, btnW, btnH);
        _sexBtn.tag = i;
        [_sexBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_sexBtn];
        [self.btnsArr addObject:_sexBtn];
        UIView  *linView = [[UIView alloc]initWithFrame:CGRectMake(_sexBtn.right-1, _userTextLab.bottom, 1, _sexBtn.height)];
        linView.backgroundColor = ColorI(0xf4f3f2);
        [self.view addSubview:linView];
        if (i == 1) {
            [_sexBtn setImage:[UIImage imageNamed:@"choice_male_pressed"] forState:UIControlStateNormal];
        }else   if (i == 2){
            [_sexBtn setImage:[UIImage imageNamed:@"choice_female_pressed"] forState:UIControlStateNormal];
            linView.backgroundColor = [UIColor clearColor];
        
        }
        
        NSString *sexType = [defaults objectForKey:@"HomeSexType"];

        if ([sexType isEqualToString:@"0"]) {
            _sexBtn.selected = (i==2);
            _sexBtn.backgroundColor = (i==2)  ? ColorI(0xf4f3f2) : [UIColor whiteColor];
            _sexStr = @"0";

        }else if ([sexType isEqualToString:@"1"]){
            _sexBtn.selected = (i==1);
            _sexBtn.backgroundColor = (i==1)  ? ColorI(0xf4f3f2) : [UIColor whiteColor];
            _sexStr = @"1";

        }else{

            _sexBtn.selected = (i==0);
            _sexBtn.backgroundColor = (i==0)  ? ColorI(0xf4f3f2) : [UIColor whiteColor];
            _sexStr = @"2";
        }
    }

    //年龄
    UILabel *agelab = [UILabel new];
    agelab.text = @"年龄";
    agelab.textColor = [UIColor blackColor];
    agelab.font = FontP(12);
    [self.view addSubview:agelab];
    [agelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_sexBtn.mas_bottom);
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(30*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(40*SCREEN_WIDTH_RATIO47);
    }];
    
    UILabel *lab2= [UILabel new];
    lab2.textColor = [UIColor blueColor];
    NSString *strminAge  = [defaults objectForKey:@"HomeMiniAge"];
    NSString *strmaxAge = [defaults objectForKey:@"HomeMaxAge"];
    if(self.minAge==nil){
        
        self.minAge = @"18";
    }
    if(self.maxAge ==nil||[self.maxAge doubleValue]>=50){
        self.maxAge = @"40";
        
        if (strmaxAge.length >0 || strminAge.length > 0) {
            lab2.text = [NSString stringWithFormat:@"%@-%@",strminAge,strmaxAge];

        }else{
            lab2.text = [NSString stringWithFormat:@"%@-%@+",self.minAge,self.maxAge];

        }
    }else{
        if (strmaxAge.length >0 || strminAge.length > 0) {
            lab2.text = [NSString stringWithFormat:@"%@-%@",strminAge,strmaxAge];

        }else{
            lab2.text = [NSString stringWithFormat:@"%@-%@",self.minAge,self.maxAge];

        }
        
    }
    
    [self.view addSubview:lab2];
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_sexBtn.mas_bottom);
        make.width.mas_equalTo(80*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(30*SCREEN_WIDTH_RATIO47);
        make.left.equalTo(agelab.mas_right);
    }];
    self.ageLabel = lab2;

    UIView *sliderView = [[UIView alloc] init];
    sliderView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:sliderView];
    [sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lab2.mas_bottom).offset(0);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(80*SCREEN_WIDTH_RATIO47);

    }];
    



    //滑动选取年龄
 
    CGFloat  currentMin;
    
    if (strminAge.length > 0) {
        currentMin = [strminAge doubleValue];
    }else{
        currentMin  = [self.minAge doubleValue];
    }

    
    CGFloat currentMax;
    
    if (strmaxAge.length > 0) {
        currentMax = [strmaxAge doubleValue];
    }else{
        currentMax= [self.maxAge doubleValue];
    
    }

    
    self.sliderAge = [MARKRangeSlider new];
    self.sliderAge.backgroundColor = [UIColor whiteColor];
    [self.sliderAge addTarget:self
                          action:@selector(rangeSliderValueDidChange:)
                forControlEvents:UIControlEventValueChanged];
    
    [self.sliderAge setMinValue:18 maxValue:40];
    
    [self.sliderAge setLeftValue:currentMin rightValue:currentMax];
    
    [self.sliderAge setRightThumbImage:[UIImage imageNamed:@"age_button"]];
    [self.sliderAge setLeftThumbImage:[UIImage imageNamed:@"age_button"]];
    [self.sliderAge setTrackImage:[UIImage imageNamed:@"rangeSliderTrack"]];
    [self.sliderAge setRangeImage:[UIImage imageNamed:@"rangeSliderTrackRange"]];
    
    self.sliderAge.minimumDistance = 0.0;
    
    
    [sliderView addSubview:self.sliderAge];
    
    [self.sliderAge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lab2.mas_bottom).offset(0);
        make.left.equalTo(sliderView).offset(15*SCREEN_WIDTH_RATIO47);
        make.right.equalTo(sliderView).offset(-15*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(80*SCREEN_WIDTH_RATIO47);
    }];
    
    UIView *priorityView = [[UIView alloc] init];
    priorityView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:priorityView];
    [priorityView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_sliderAge.mas_bottom).offset(10);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(80*SCREEN_WIDTH_RATIO47);
    }];
    
    
    
    UILabel *priorityLab = [[UILabel alloc] init];
    priorityLab.text = @"优先看最新活跃的";
    priorityLab.textColor = ColorI(0x2e353f);
    priorityLab.font = FontP(15*SCREEN_WIDTH_RATIO47);
    [priorityView addSubview:priorityLab];
    [priorityLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(priorityView).offset(15*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(80*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(KScreenWidth/2);
        make.top.equalTo(priorityView.mas_top);
        
    }];
    
    
    

    _SwitchBtn = [[UISwitch alloc] init];
    [priorityView addSubview:_SwitchBtn];
    [_SwitchBtn addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];   // 开关事件切换通知
    _SwitchBtn.onTintColor = Colorbackground;
    
    NSString *str = [defaults objectForKey:@"HomeswitchButtonOn"];
    
    if ([str isEqualToString:@"0"]) {
        _SwitchBtn.on = NO;

    }else{
        _SwitchBtn.on = YES;
    }
    
    

    
    
    [_SwitchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(priorityView).offset(-45*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(20*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(40*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(priorityView.mas_top).offset(25*SCREEN_WIDTH_RATIO47);
    }];
    
}




- (void)switchAction:(id)sender{
    UISwitch *switchButton = (UISwitch *)sender;
    
    BOOL isButtonOn = [switchButton isOn];
    
    
        if (isButtonOn) {
            _priorityStr = @"1";
            NSLog(@"开%ld",(long)switchButton.tag);
            
            
            
        }else {
            _priorityStr = @"0";
            NSLog(@"关%ld",(long)switchButton.tag);
            
        }
    
    [defaults removeObjectForKey:@"HomeswitchButtonOn"];
    [defaults setObject:_priorityStr forKey:@"HomeswitchButtonOn"];
    [defaults synchronize];
 
}



//选择年龄
- (void)rangeSliderValueDidChange:(MARKRangeSlider *)slider
{
    if([slider isEqual:self.sliderAge]){
        NSString *str = [NSString stringWithFormat:@"%0.0f-%0.0f",
                         slider.leftValue,slider.rightValue];
        _minAge = [NSString stringWithFormat:@"%.0f",slider.leftValue];
        _maxAge = [NSString stringWithFormat:@"%.0f",slider.rightValue];

        //存储选择值 下次进入时进行展示
        [defaults removeObjectForKey:@"HomeMiniAge"];
        [defaults removeObjectForKey:@"HomeMaxAge"];
        
        [defaults setObject:_minAge forKey:@"HomeMiniAge"];
        [defaults setObject:_maxAge forKey:@"HomeMaxAge"];
        [defaults synchronize];
        
        
        if(slider.rightValue==40.0){
            str = [NSString stringWithFormat:@"%@+",
                   str];
        }
        self.ageLabel.text  =str;
    }
}
//选择性别
- (void)btnClick:(UIButton *)sender{
    
    for (UIButton * button in self.btnsArr) {
        
        button.selected = (button == sender);
        
        if (button.selected) {
            button.backgroundColor = ColorI(0xf4f3f2);
        }else{
            button.backgroundColor = [UIColor whiteColor];
        }
    }
    if (sender.tag == 0) {
        _sexStr = @"2";
        
    }else if (sender.tag == 1){
        _sexStr = @"1";
    }else{
        _sexStr = @"0";
    }
    [defaults removeObjectForKey:@"HomeSexType"];
    [defaults setObject:_sexStr forKey:@"HomeSexType"];
    [defaults synchronize];

}

//取消返回
- (void)back:(UIButton *)button{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)determineBtn:(UIButton *)button{
    
    NSString *miniStr = [defaults objectForKey:@"HomeMiniAge"];
    NSString *maxStr = [defaults objectForKey:@"HomeMaxAge"];
    if (self.presentjump){
        
        
        if (miniStr.length > 0) {
            _minAge = miniStr;
        }
        if (maxStr.length > 0) {
            _maxAge = maxStr;
        }
        
        
        
        self.presentjump(_minAge, _maxAge, _priorityStr, _sexStr);
        NSLog(@"最小年龄===============%@\n最大==========%@\n优先级=========%@\n性别============%@\n",_minAge,_maxAge,_priorityStr,_sexStr);
    
    }
    [self dismissViewControllerAnimated:YES completion:nil];

}

@end
